package com.lindenvalley.de.adapter;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.lindenvalley.de.R;
import com.lindenvalley.de.content.Section;
import com.lindenvalley.de.database.DbSectionsHelper;



public class ThemeArrayAdapter extends ArrayAdapter<Section> implements View.OnTouchListener{
	
	private Context mContext;
	private LayoutInflater inflater;
	private final String TAG = this.getClass().getSimpleName();
	private final String ROUND_NAME = "Runde ";
	private List<Section> sections;
	private int resource;
	private DbSectionsHelper sectionsHelper;
	private SQLiteDatabase readDatabase;
	private String[] columns = new String[]{DbSectionsHelper.ROUND, "count(" + DbSectionsHelper.QUESTION + ")"};
	private int isExamMode;

	public ThemeArrayAdapter(Context context, int resource, List<Section> sections){
		super(context, resource);
		this.mContext = context;
		this.resource = resource;
		this.sections = sections;
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		sectionsHelper = new DbSectionsHelper(mContext);
		readDatabase = sectionsHelper.getReadableDatabase();		
	}

	@Override
	public int getCount() {
		return sections != null ? sections.size() : 0;
	}
		
	@Override
	public Section getItem (int position){
		try{
			return sections != null ? sections.get(position) : null;
		} catch (IndexOutOfBoundsException ex){
			return null;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		Log.i(TAG, ">>>>>>>>> getView(" + position + ", " + convertView + ", " + parent + ")");
						
		if (sections == null)
			return null;		
		
		if (convertView == null){
			convertView = inflater.inflate(resource, parent, false);
		}
		Section section = sections.get(position);
		setRatingsBar(convertView, section);

		TextView themeView = (TextView) convertView.findViewById(R.id.mainThemeName);
		themeView.setText(section.getName());		
		
		return convertView;
	}
	
	public void setTasks(List<Section> tasks){
		this.sections = tasks;
		notifyDataSetChanged();
		notifyDataSetInvalidated();
	}
		
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {

//		Log.i(TAG, ">>>>>>>>>>>>>>> Touch " + event.getAction());
		return true;
	}
	
	private void setRatingsBar(View view, Section section){
		Cursor cursor = readDatabase.query(
				DbSectionsHelper.TABLE_NAME, 
				columns, 
				DbSectionsHelper.THEME_ID + "=? and " + DbSectionsHelper.IS_EXAM + "=" + isExamMode, 
				new  String[]{String.valueOf(section.getId())}, 
				DbSectionsHelper.ROUND, null, DbSectionsHelper.ROUND);
		
		TextView fragenView = (TextView) view.findViewById(R.id.mainFragenView);
		TextView themeView = (TextView) view.findViewById(R.id.mainThemeName);
		
		RatingBar bar1 = (RatingBar) view.findViewById(R.id.ratingBar1);
		RatingBar bar2 = (RatingBar) view.findViewById(R.id.ratingBar2);
		RatingBar bar3 = (RatingBar) view.findViewById(R.id.ratingBar3);
		RatingBar bars[] = new RatingBar[3];
		bars[0] = bar1;
		bars[1] = bar2;
		bars[2] = bar3;
		
		TextView round1 = (TextView) view.findViewById(R.id.RoundView1);
		TextView round2 = (TextView) view.findViewById(R.id.RoundView2);
		TextView round3 = (TextView) view.findViewById(R.id.RoundView3);
		
		TextView rounds[] = new TextView[3];
		rounds[0] = round1;
		rounds[1] = round2;
		rounds[2] = round3;
		int firstRowRound = 0;
		if (cursor.getCount() > 3)
			firstRowRound = Math.abs(cursor.getCount() - 3);		
		
		int round = 1;
		int questions = 0;
		cursor.moveToPosition(firstRowRound);
		int count = cursor.getCount() - cursor.getPosition();
		int valuePercent = 0;
		for (int i = 0; i < count; i++){
			cursor.moveToPosition(firstRowRound);
			questions = 0;
			try{
				round = cursor.getInt(0);
				questions = cursor.getInt(1);
			} catch (CursorIndexOutOfBoundsException ex){
				
			}
			valuePercent =questions*100/section.getQuestion().size();
			if (valuePercent <= 33){
				bars[i].setRating(1);
			} else if (valuePercent <100){
				bars[i].setRating(2);
			} else {
				bars[i].setRating(3);
			}
			
			rounds[i].setText(ROUND_NAME + round + ":");
			firstRowRound++;
			round++;			
		} 
		if (count == 0){
			bars[0].setRating(1);
			bars[1].setRating(1);
			bars[2].setRating(1);
		}
		
		if (valuePercent < 100){
			themeView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
		} else {
			themeView.setCompoundDrawablesWithIntrinsicBounds(null, null, mContext.getResources().getDrawable(R.drawable.marker), null);
		}
		
		fragenView.setText(questions + "/" + section.getQuestion().size());
		
		cursor.close();
	}
	
	public void setIsExamMode(boolean isExam){
		if (isExam)
			isExamMode  = 1;
		else
			isExamMode = 0;
		notifyDataSetChanged();
	}
		

}
